//package com.allwees.bs.task.order.auto;
//
//import com.allwees.bs.core.model.order.config.ScheduleTimeProperties;
//import com.allwees.bs.core.model.order.wf.refund.ERefundState;
//import com.allwees.bs.module.oms.dao.OmsOrderDao;
//import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
//import com.allwees.bs.module.oms.dao.OrderRefundDao;
//import com.allwees.bs.module.oms.entity.OmsOrder;
//import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
//import com.allwees.bs.module.oms.wf.EOmsOrderAction;
//import com.allwees.core.common.util.DateUtil;
//import com.xxl.job.core.biz.model.ReturnT;
//import com.xxl.job.core.handler.annotation.XxlJob;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import org.springframework.stereotype.Service;
//import org.springframework.util.CollectionUtils;
//
//import java.util.Date;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//@Slf4j
//@Service
//
//public class OrderIntoSettleTask {
//
//    @Autowired
//    private OmsOrderDao omsOrderDao;
//    @Autowired
//    private OmsOrderItemDao omsOrderItemDao;
//    @Autowired
//    private OrderRefundDao orderRefundDao;
//    @Autowired
//    private GeneralOmsOrderItemService generalOmsOrderItemService;
//    @Autowired
//    private ScheduleTimeProperties scheduleTimeProperties;
//
//    @XxlJob(value="orderIntoSettleTask")
//    public ReturnT<String> run(String s) {
//        doTask();
//        return ReturnT.SUCCESS;
//    }
//
//    private void doTask() {
//        if (scheduleTimeProperties.getOrderAutoToSettleOnReceivedSecond() == null) {
//            return;
//        }
//        List<OmsOrder> omsOrders = omsOrderDao.findExpireToSettleUuids(DateUtil.addSeconds(new Date(), -scheduleTimeProperties.getOrderAutoToSettleOnReceivedSecond()));
//        if (CollectionUtils.isEmpty(omsOrders)) {
//            return;
//        }
//
//        Set<String> orderUuids = new HashSet<>();
//        omsOrders.forEach(omsOrder->{
//            if (orderRefundDao.getCountByOrderUuidAndState(omsOrder.getUuid(), ERefundState.REFUND_REQUESTED) == 0) {
//                orderUuids.add(omsOrder.getUuid());
//            }
//        });
//
//        List<String> omsOrderItemUuids = omsOrderItemDao.findUuidsByOrderUuids(orderUuids);
//        if(CollectionUtils.isEmpty(omsOrderItemUuids)){
//            return;
//        }
//
//        generalOmsOrderItemService.doSettle(new HashSet<>(omsOrderItemUuids));
//        omsOrders.forEach(omsOrder -> {
//            omsOrder.signal(EOmsOrderAction.SYSTEM_JOIN_SETTLE);
//            omsOrderDao.updateByUuid(omsOrder, omsOrder.getUuid());
//        });
//    }
//}
